package guoanmaker.provider.business.controller;

import guoanmaker.provider.business.model.OperatorServerClass;
import guoanmaker.provider.business.modelnobind.ProvideServerClassPC;
import guoanmaker.provider.business.service.OperatorServerClassService;
import guoanmaker.provider.helper.common.AjaxJson;
import guoanmaker.provider.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Light on 2017/6/22.
 */
@RestController
@RequestMapping(ApiController.SERVER_CLASS_URL)
public class ProviderServerClassController {

    private final OperatorServerClassService provideServerClassService;

    @Autowired
    public ProviderServerClassController( OperatorServerClassService provideServerClassService) {
        this.provideServerClassService = provideServerClassService;
    }

    /**
     *  
     *
     * @return AjaxJson   
     * @Title: saveServerClass 
     * @Description: 保存服务分类信息，包括新增与修改 
     * @param: ProvideServerClass
     * @throws 
     */

    @ApiOperation(value = "保存服务分类接口", notes = "保存服务分类信息，包括新增与修改 ")
    @PostMapping(value = "saveServerClass")
    public AjaxJson saveServerClass(@RequestBody OperatorServerClass provideServerClass) {
    	AjaxJson ajaxJson=new AjaxJson();
    	if (provideServerClass.getServerLevel() == 0)
            provideServerClass.setParentServerId(null);
        else if (StringUtils.isEmpty(provideServerClass.getParentServerId())) {
            ajaxJson.setSuccess(false).setStatus(200).setData(provideServerClass).setMsg("非一级分类请正确填写父分类");
            return ajaxJson;
        }
        provideServerClass = provideServerClassService.saveServerClass(provideServerClass);
        ajaxJson.setSuccess(true).setStatus(200).setData(provideServerClass);
        return ajaxJson;
    }

    /**
     *  
     *
     * @param    
     * @return AjaxJson   
     * @Title: getAllProviderClass
     * @Description: 获取全部服务分类列表
     * @throws 
     */
    @ApiOperation(value = "获取全部服务分类列表", notes = "获取全部服务分类列表")
    @GetMapping(value = "getServerClass")
    public AjaxJson getAllServerClass() {
    	AjaxJson ajaxJson=new AjaxJson();
        List<OperatorServerClass> provideServerClassList = provideServerClassService.getAllServerClass();
        ajaxJson.setSuccess(true).setStatus(200).setData(provideServerClassList);
        return ajaxJson;
    }

    /** 
    * @Title: getProviderClassByPage
    * @Description: 分页查询服务分类
    * @param: page  当前页
     * @param: size 每页条数
    * @return AjaxJson  
    * @throws 
    */
    @ApiOperation(value = "分页查询服务分类", notes = "分页查询服务分类")
    @GetMapping(value = "getServerClassByPage")
    public AjaxJson getServerClassByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                           @RequestParam(value = "size", defaultValue = "10") Integer size) {
    	AjaxJson ajaxJson=new AjaxJson();
    	ajaxJson.setSuccess(true).setStatus(200).setData(provideServerClassService.getServerClassByPage(page,size));
        return ajaxJson;
    }

    /** 
    * @Title: getServerClassById
    * @Description: 根据ID查找服务分类
    * @param: id  
    * @return AjaxJson
    * @throws 
    */
    @ApiOperation(value = "根据ID查找服务分类", notes = "根据ID查找服务分类")
    @GetMapping(value = "getServerClassById")
    public AjaxJson getServerClassById(@RequestParam(value = "id")String id) {
    	AjaxJson ajaxJson=new AjaxJson();
        ajaxJson.setSuccess(true).setStatus(200).setData(provideServerClassService.getServerClassById(id));
        return ajaxJson;
    }

    /**
     *
     * @return AjaxJson    
     * @Title: 根据服务分类级别查询并分页
     * @Description: ${todo} 
     * @param: page  当前页
     * @param: size 每页条数
     * @param: serverLevel  服务分类级别
     * @throws 
     */
    @ApiOperation(value = "根据服务分类级别查询", notes = "根据服务分类级别查询并分页")
    @GetMapping(value = "getServerClassByLevel")
    public AjaxJson getServerClassByLevel(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                          @RequestParam(value = "size", defaultValue = "2") Integer size,
                                          @RequestParam(value = "serverLevel") Integer serverLevel) {
    	AjaxJson ajaxJson=new AjaxJson();
    	ajaxJson.setSuccess(true).setStatus(200).setData(provideServerClassService.getServerClassByServerLevel(page, size, serverLevel));
        return ajaxJson;
    }

    /** 
    * @Title: getServerClassByParentId
    * @Description: 根据父项Id，查找所有子项 
    * @param: String parentServerId 父项Id  
    * @return AjaxJson    
    * @throws 
    */

    @ApiOperation(value = "查找子项", notes = "根据父项Id，查找所有子项")
    @GetMapping(value = "getServerClassByParentId")
    public AjaxJson getServerClassByParentId(@RequestParam(value = "parentServerId")String parentServerId) {
    	AjaxJson ajaxJson=new AjaxJson();
    	ajaxJson.setSuccess(true).setStatus(200).setData(provideServerClassService.getServerClassByParentServerId(parentServerId));
    	return ajaxJson;
    }

    /** 
     * @Title: deleteServerClass
     * @Description: 根据主键删除服务分类
     * @param: id  
     * @return AjaxJsone
     * @throws 
     */
    @ApiOperation(value = "删除服务分类", notes = "根据主键删除服务分类")
    @DeleteMapping(value = "delete/{id}")
    public AjaxJson deleteServerClass(@PathVariable(value = "id") String id) {
    	AjaxJson ajaxJson=new AjaxJson();
        provideServerClassService.deleteServerClass(id);
        return ajaxJson.setSuccess(true).setStatus(200).setData("数据删除成功！");
    }
    /**
     * 
    * @Title: getServerClassByListForlevid 
    * @Description: TODO(这里用一句话描述这个方法的作用) 
    * @param serverLevel
    * @return    设定文件 
    * @return AjaxJson    返回类型 
    * @throws 
     */
    @ApiOperation(value = "根据服务分类级别查询", notes = "根据服务分类级别查询")
    @RequestMapping(value = "/getServerClassByListForlevid", method = RequestMethod.POST)
    public AjaxJson getServerClassByListForlevid() {
    	AjaxJson ajaxJson=new AjaxJson();
    	Integer serverLevel=1;
    	List<OperatorServerClass> pss=provideServerClassService.getServerClassListForlevid(serverLevel);
    	serverLevel=2;
    	List<OperatorServerClass> pssTwo=provideServerClassService.getServerClassListForlevid(serverLevel);
    	Map<String,Object> ss=new HashMap<>();
    	ss.put("pss", pss);
    	ss.put("pssTwo", pssTwo);
    	if(pss.size()>0&&pssTwo.size()>0){
    		ajaxJson.setSuccess(true).setStatus(200).setData(ss);
    	}else{
    		ajaxJson.setSuccess(false).setStatus(250).setMsg("请联系管理员添加数据");
    	}
    	
        return ajaxJson;
    }

    /** 
     * @Title: getServerClassByBusinessCon
     * @Description: 根据商品所选择的服务类别查询分类
     * @param: businessId 商户id 
     * @return AjaxJson    
     * @throws 
     */

     @ApiOperation(value = "查找服务商提供的服务范围", notes = "根据服务商的服务范围查找所有类别分类")
     @RequestMapping(value = "/getServerClassByBusinessCon", method = RequestMethod.POST)
     public AjaxJson getServerClassByBusinessCon(String businessId) {
    	 AjaxJson ajaxJson=new AjaxJson();
    	 Integer serverLevel=2;
    	 Map<String, Object> remap=provideServerClassService.selectCommodityservice(businessId);
         ajaxJson.setSuccess(true).setStatus(200).setData(remap);
         return ajaxJson;
     }
     
     
     /**
     *
     * @return AjaxJson    
     * @Title: 根据服务分类级别查询
     * @Description: ${todo} 
     * @param: serverLevel  服务分类级别
     * @throws 
     */
    @ApiOperation(value = "根据服务分类级别查询", notes = "根据服务分类级别查询并分页")
    @RequestMapping(value = "/getServerClassByServiceLevel", method = RequestMethod.POST)
    public AjaxJson getServerClassByServiceLevel( Integer serverLevel) {
    	AjaxJson ajaxJson=new AjaxJson();
    	ajaxJson.setSuccess(true).setStatus(200).setData(provideServerClassService.getServerClassByServerLevel( serverLevel));
        return ajaxJson;
    }
     
    /** 
     * @Title: findByIdInAndParentServerIdIs
     * @Description: 根据商品所选择的服务类别查询分类
     * @param: businessId 商户id 
     * @return AjaxJson    
     * @throws 
     */
     @ApiOperation(value = "查找服务商提供的服务范围", notes = "根据服务商的服务范围查找一级分类")
     @RequestMapping(value = "/selectBusinessService", method = RequestMethod.POST)
     public AjaxJson selectBusinessService(String businessId) {
    	 AjaxJson ajaxJson=new AjaxJson();
    	 Map<String, Object> remap=provideServerClassService.selectBusinessService(businessId);
         ajaxJson.setSuccess(true).setStatus(200).setData(remap);
         return ajaxJson;
     }
}
